package com.tudouji.project.handover.service.impl;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

import com.flow.service.mapper.IFlowTaskDetailMapper;
import com.flow.service.pojo.flow.FlowTaskDetail;
import com.flow.service.process.service.impl.FlowProcessProxy;
import com.tudouji.common.utils.DateUtils;
import com.tudouji.common.utils.SecurityUtils;
import com.tudouji.common.utils.StringUtils;
import com.tudouji.framework.web.domain.AjaxResult;
import com.tudouji.project.flow.domain.FlowTask;
import com.tudouji.project.flow.mapper.FlowTaskMapper;
import com.tudouji.project.handover.domain.HandoverOrder;
import com.tudouji.project.handover.mapper.HandoverOrderMapper;
import com.tudouji.project.noticeinfo.domain.Noticeinfo;
import com.tudouji.project.noticeinfo.mapper.NoticeinfoMapper;
import com.tudouji.project.system.domain.SysUser;
import com.tudouji.project.system.mapper.SysUserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.tudouji.project.handover.mapper.HandoverRecordMapper;
import com.tudouji.project.handover.domain.HandoverRecord;
import com.tudouji.project.handover.service.IHandoverRecordService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 工作移交记录Service业务层处理
 * 
 * @author tudouji
 * @date 2022-06-20
 */
@Service
public class HandoverRecordServiceImpl implements IHandoverRecordService
{
    @Autowired
    private HandoverRecordMapper handoverRecordMapper;

//    通知记录mapper接口
    @Autowired
    private NoticeinfoMapper noticeMapper;
//流程任务mapper接口
    @Autowired
    private FlowTaskMapper taskMapper;
//系统用户表mapper接口
    @Autowired
    private SysUserMapper userMapper;
//工作交接单mapper接口
    @Autowired
    private HandoverOrderMapper handoverOrderMapper;

    @Autowired
    private IFlowTaskDetailMapper flowTaskDetailMapper;

    @Autowired
    private  FlowProcessProxy flowProcessProxy;


//    @Autowired
//    private FlowTaskDetailVoMapper flowTaskDetailVoMapper;
    /**
     * 查询工作移交记录
     * 
     * @param id 工作移交记录ID
     * @return 工作移交记录
     */
    @Override
    public HandoverRecord selectHandoverRecordById(Long id)
    {
        return handoverRecordMapper.selectHandoverRecordById(id);
    }

    /**
     * 查询工作移交记录列表
     * 
     * @param handoverRecord 工作移交记录
     * @return 工作移交记录
     */
    @Override
    public List<HandoverRecord> selectHandoverRecordList(HandoverRecord handoverRecord)
    {
        return handoverRecordMapper.selectHandoverRecordList(handoverRecord);
    }

    /**
     * 新增工作移交记录
     * 
     * @param handoverRecord 工作移交记录
     * @return 结果
     */
    @Override
    public int insertHandoverRecord(HandoverRecord handoverRecord)
    {
        handoverRecord.setCreateTime(DateUtils.getNowDate());
        return handoverRecordMapper.insertHandoverRecord(handoverRecord);
    }

    /**
     * 修改工作移交记录
     * 
     * @param handoverRecord 工作移交记录
     * @return 结果
     */
    @Override
    public int updateHandoverRecord(HandoverRecord handoverRecord)
    {
        handoverRecord.setUpdateTime(DateUtils.getNowDate());
        return handoverRecordMapper.updateHandoverRecord(handoverRecord);
    }

    /**
     * 批量删除工作移交记录
     * 
     * @param ids 需要删除的工作移交记录ID
     * @return 结果
     */
    @Override
    public int deleteHandoverRecordByIds(Long[] ids)
    {
        return handoverRecordMapper.deleteHandoverRecordByIds(ids);
    }

    /**
     * 删除工作移交记录信息
     * 
     * @param id 工作移交记录ID
     * @return 结果
     */
    @Override
    public int deleteHandoverRecordById(Long id)
    {
        return handoverRecordMapper.deleteHandoverRecordById(id);
    }


    @Override
    @Transactional
    public AjaxResult trans(Long fromUserId, Long toUserId, String remark,String ftdIDs) {
//        创建人
        SysUser operator= SecurityUtils.getLoginUser().getUser();
//        交出人
        SysUser fromUser=userMapper.selectUserById(fromUserId);
//        接交人
        SysUser toUser=userMapper.selectUserById(toUserId);

        //获取待办
//        List<Noticeinfo> notices=noticeMapper.selectNuHandle(fromUserId);

        //更新flowtask中 代办人    写入交接人名称和接受人名称
//        FlowTask task=new FlowTask();
//        task.setFromUser(fromUser.getNickName());
//        task.setToUser(toUser.getNickName());


        //更新flowtask中 代办人
        //20230215 交接代办的时候不需要更新flow_task
        //FlowTask task=new FlowTask();
        //task.setFromUser(fromUser.getNickName());
        //task.setToUser(toUser.getNickName());
        //task.setRecieverId(fromUserId);
        //taskMapper.updateTaskByUserId(task);
        //20230215 交接代办的时候不需要更新flow_task


//        更新处理人     写入处理人id
//        task.setRecieverId(fromUserId);
//        taskMapper.updateTaskByUserId(task);

        //更新待办
//        FlowTaskDetail flowTaskDetail = new FlowTaskDetail();
//        flowTaskDetail.setRecieverId(fromUserId);
//        List<FlowTaskDetail> notices = flowTaskDetailMapper.selectNuHandleInfo(flowTaskDetail);
//        flowTaskDetail.setToUserId(toUserId);
//        flowTaskDetailMapper.updateUnHandleInfoByUserId(flowTaskDetail);


        if(StringUtils.isBlank(ftdIDs)){
            return AjaxResult.error("请选择需移交的待处理任务！");
        }
        flowProcessProxy.transferFtd(fromUserId,fromUser.getNickName(),toUserId,toUser.getNickName(),ftdIDs);
        //写入交接记录
        HandoverOrder order=new HandoverOrder();
//        更新交出人id
        order.setTransfromId(fromUserId);
//        更新交出人
        order.setTransfromName(fromUser.getNickName());
//        更新接交人id
        order.setTranstoId(toUserId);
//        更新接交人
        order.setTranstoName(toUser.getNickName());
//        更新交接状态
        order.setStatus(0);
        order.setCreateBy(operator.getUserId());
        order.setCreateName(operator.getNickName());
        order.setCreateTime(new Date());
        order.setRemark(remark);
        handoverOrderMapper.insertHandoverOrder(order);

        List<String> flowTaskIds = Arrays.asList(ftdIDs.split(","));

        for(String ftdId:flowTaskIds){
            HandoverRecord record=new HandoverRecord();
            record.setHoOrderId(order.getId());
            record.setTransfromName(fromUser.getNickName());
            record.setTranstoId(toUserId);
            record.setTranstoName(toUser.getNickName());
            //20230216交接记录中没有记录flowTaskId，只记录了flow_task_detail的Id
            //record.setFlowtaskId(in.getFlowTaskId());
            record.setNoticeinfoId(Long.valueOf(ftdId));
            record.setCreateBy(operator.getUserId());
            record.setCreateName(operator.getNickName());
            record.setCreateTime(new Date());
            handoverRecordMapper.insertHandoverRecord(record);
        }
        return AjaxResult.success();
    }
}
